package com.blacktea.behavioralpatterns.observerpattern.service;

import com.blacktea.behavioralpatterns.observerpattern.Order;
import com.blacktea.behavioralpatterns.observerpattern.OrderStatus;
import com.blacktea.behavioralpatterns.observerpattern.OrderStatusChangeEvent;
import lombok.RequiredArgsConstructor;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;

/**
 * @description: 订单服务类
 * @author: black tea
 * @date: 2023/3/25 22:50
 */
@Service
@RequiredArgsConstructor
public class OrderService {

    private final ApplicationEventPublisher publisher;

    public void updateOrderStatus(Order order, OrderStatus newStatus){
        // 更新订单状态
        order.setStatus(newStatus);
        // 发布订单状态变化事件
        publisher.publishEvent(new OrderStatusChangeEvent(this, order));
    }

}
